# -*- coding: utf-8 -*-
"""
Created on Wed Aug 29 11:09:51 2018

@author: Who

聚富财经
https://www.jfq.com/

"""
import requests
import re
import time
from WriteData import writedata
from Tk import genearteMD5


def get_html_text(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
    try:
        r = requests.get(url, timeout=30, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except Exception as e:
        print(e)
        return None


def get_dates_zx(url):
    text = get_html_text(url)
    if not text:
        return None
    regex = re.compile(r'<span class="time fr">([\s\S]*?)</span>')
    dates = re.findall(regex, text)
    return dates


def get_dates_kx():
    a = 1
    dates = []
    while a < 30:
        s = '{}年{}月{}日'
        s = s.format(time.strftime("%Y", time.localtime()), time.strftime("%m", time.localtime()),
                     time.strftime("%d", time.localtime()))
        url = 'https://www.jfq.com/live/?type=ajax&page={}&catid=0&starttime={}&t=1535513059000'.format(a, s)
        text = get_html_text(url)
        if not text:
            continue
        print('----聚富财经快讯获取第{}页----'.format(a))
        regex = re.compile(r'<i></i>([\s\S]*?)</label>')
        dates += re.findall(regex, text)
        a += 1
    return dates


def count_process_one():
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 0
    h = time.strftime("%H", time.localtime())
    while 1:
        url = 'https://www.jfq.com/news/?type=ajax&page={}&catdir=news&t=1535511725000'.format(a)
        dates = get_dates_zx(url)
        if not dates:
            break
        for date in dates:
            if '分钟前' in date:
                d1 += 1
            elif ('小时前' in date) and (date[:-3] < h):
                d1 += 1
            elif ('小时前' in date) and (date[:-3] >= h):
                d2 += 1
            elif '1天前' in date:
                d2 += 1
            elif '2天前' in date:
                d3 += 1
            else:
                d4 += 1
        a += 1
        print('----聚富财经资讯获取第{}页----'.format(a))
        if d4 != 0:
            break
    return (d1, d2, d3)


def count_process_two():  # 快讯
    d1 = 0
    d2 = 0
    d3 = 0
    a = 0
    l = []
    dates = get_dates_kx()
    try:
        while 1:
            if dates[a] < dates[a + 1]:
                l.append(a + 1)
            if len(l) == 3:
                break
            a += 1
        d1 = len(dates[:l[0]])
        d2 = len(dates[l[0]:l[1]])
        d3 = len(dates[l[1]:l[2]])
        return (d1, d2, d3)
    except:
        return (d1, d2, d3)


def main():
    u = '聚富财经'
    t1 = count_process_one()
    t2 = count_process_two()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0] + t2[0], "yesterday": t1[1] + t2[1], "frontday": t1[2] + t2[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
